package com.zhentao.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.zhentao.file.service.impl.FileStorageServiceImpl;
import com.zhentao.mapper.WmMaterialMapper;
import com.zhentao.mapper.WmNewsMapper;
import com.zhentao.mapper.WmNewsMaterialMapper;
import com.zhentao.model.dtos.PageResponseResult;
import com.zhentao.model.dtos.ResponseResult;
import com.zhentao.model.enums.AppHttpCodeEnum;
import com.zhentao.model.wemedia.dto.WmMaterialDto;
import com.zhentao.model.wemedia.pojo.WmMaterial;
import com.zhentao.service.WmMaterialService;
import com.zhentao.utils.common.thread.WmThreadLocalUtil;
import lombok.SneakyThrows;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import java.util.Date;
import java.util.UUID;

@Service
public class WmMaterialServiceImpl implements WmMaterialService {
    @Autowired
    private FileStorageServiceImpl fileStorageService;

    @Autowired
    private WmMaterialMapper wmMaterialMapper;



    //自媒体端素材上传
    @SneakyThrows
    @Override
    public ResponseResult uploadPicture(MultipartFile multipartFile) {
        //参数校验
        if(multipartFile == null || multipartFile.getSize() <= 0){
            //无效参数
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_INVALID);
        }

        //将 文件 上传到minio
        //防止文件名不冲突
        String fileName = UUID.randomUUID().toString().replace("-", "");
        String originalFilename = multipartFile.getOriginalFilename();
        String substring = originalFilename.substring(originalFilename.lastIndexOf("."));

        String fileId = fileStorageService.uploadImgFile
                (fileName + substring, multipartFile.getInputStream());
        //创建素材对象 WmMaterial
        WmMaterial wmMaterial = new WmMaterial();
        wmMaterial.setUserId(WmThreadLocalUtil.getUser().getId());
        wmMaterial.setUrl(fileId);
        wmMaterial.setIsCollection((short) 0);//0 不收藏
        wmMaterial.setType((short) 0);//0 图片  1 视频
        wmMaterial.setCreatedTime(new Date());//创建时间
        //将数据存到数据库中
        wmMaterialMapper.insert(wmMaterial);
        //返回结果
        return ResponseResult.okResult(wmMaterial);
    }

    //分页查询 素材列表
    @Override
    public ResponseResult list(WmMaterialDto wmMaterialDto) {
        //1.检查参数
        wmMaterialDto.checkParam();;

        //2.分页查询
        IPage page = new Page(wmMaterialDto.getPage(),wmMaterialDto.getSize());
        LambdaQueryWrapper<WmMaterial> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        //是否收藏
        if(wmMaterialDto.getIsCollection() != null && wmMaterialDto.getIsCollection() == 1){
            //==1 是收藏
            lambdaQueryWrapper.eq(WmMaterial::getIsCollection,wmMaterialDto.getIsCollection());
        }

        //用户查询
        lambdaQueryWrapper.eq(WmMaterial::getUserId,WmThreadLocalUtil.getUser().getId());

        //按照时间排序  倒序排序
        lambdaQueryWrapper.orderByDesc(WmMaterial::getCreatedTime);

        page = wmMaterialMapper.selectPage(page, lambdaQueryWrapper);
        //3.返回结果
        PageResponseResult pageResponseResult = new PageResponseResult(wmMaterialDto.getPage(),wmMaterialDto.getSize(), (int) page.getTotal());
//        pageResponseResult.setSize(Integer.parseInt(page.getSize()+""));
//        pageResponseResult.setTotal(Integer.valueOf(page.getTotal()+""));
//        pageResponseResult.setCurrentPage(Integer.valueOf(page.getCurrent()+""));
//        pageResponseResult.setData(page.getRecords());
//        pageResponseResult.setCode(200);
//        pageResponseResult.setErrorMessage("查询成功");
        pageResponseResult.setData(page.getRecords());

        return pageResponseResult;
    }
}














